from django.shortcuts import render, HttpResponse, redirect
from app01 import models
from django import forms
import os, datetime, time, json


# Create your views here.

def login(request):
    print("login------->", request.GET)
    global off_total
    phone = request.GET.get("phone")
    pwd = request.GET.get("pwd")
    off_total = request.GET.get("off_total")
    id = request.GET.get("id")
    c = models.MemberList.objects.filter(phone=phone).count()
    if c and pwd:
        models.RecorderList.objects.filter(id = id).update(p_phone=phone, p_off_total=off_total)
        return HttpResponse("OK")
    elif phone == "" and pwd == "":
        return HttpResponse("OK")
    else:
        return HttpResponse("NO")


###################################################
def register(request):
    print("register------>", request.GET)
    global off_total
    phone = request.GET.get("r_phone")
    pwd = request.GET.get("r_pwd")
    off_total = request.GET.get("off_total")
    c = models.MemberList.objects.filter(phone=phone).count()
    if not phone:
        return HttpResponse("NULL")
    if c:
        return HttpResponse("NO")
    else:
        models.MemberList.objects.create(
            phone=phone,
            pwd=pwd,
            reg_date=times
        )
        models.RecorderList.objects.filter(p_times=times).update(p_phone=phone)
        return HttpResponse("OK")
        ##################################################
        # def diancan(request):
        #    if request.method == "GET":
        #        obj = models.DishesList.objects.all()
        #        for obj_list in obj:
        # print(j.pro)


#            return render(request, "diancan.html", {"foodlist":obj_list})
##################################################
def ajax(request):
    print("ajax------>", request.POST)
    global total, p_pro, p_en_pro, p_price, p_result, p_sum, times, off_total
    if request.method == 'POST':
        data = request.POST.getlist('data[]')

        recorder = models.RecorderList.objects.create()

        result = 0
        for item in data:
            dict_item = json.loads(item)
            id = dict_item["id"]
            num = dict_item['num']
            food = models.FoodList.objects.get(id=id)
            total = food.price * int(num)

            models.BillList.objects.create(
                p_pro=food,
                p_num=num,
                p_sum=total,
                p_recorder=recorder,
            )
            result += total

        models.RecorderList.objects.filter(id=recorder.id).update(
            p_result=result,
        )
        return HttpResponse(json.dumps({"result": 0, 'msg': "OK", 'id': recorder.id}), content_type="application/json")

    # off_total=request.GET.get("off_total")
    # print(off_total)
    # total = request.GET.get("total")
    # p_pro=request.GET.get("p_pro")
    # p_en_pro=request.GET.get("p_en_pro")
    # p_price=request.GET.get("p_price")
    # p_result=request.GET.get("p_result")
    # p_sum=request.GET.get("p_sum")
    # times = time.ctime()
    # models.RecorderList.objects.create(
    #     p_pro=p_pro,
    #     p_en_pro=p_en_pro,
    #     p_price=p_price,
    #     p_result=p_result,
    #     p_sum=p_sum,
    #     p_times=times
    # )
    return HttpResponse(json.dumps({"result": 1, 'msg': "error"}), content_type="application/json")


####################################################
def choujiang(request):
    print("choujiang----->", request.GET)
    if request.method == "GET":
        id = request.GET.get("id")
        total = models.RecorderList.objects.get(id = id).p_result
        return render(request, "choujiang.html", {"id": id, "total": total})


########################################################
def dayin(request):
    print("dayin------->", request.GET)
    if request.method == "GET":
        id = request.GET.get("id")
        recorder = models.RecorderList.objects.get(id=id)

        off = recorder.p_result if (recorder.p_phone is None) else off_total

        bills = models.BillList.objects.filter(p_recorder = recorder)

        obj_k = [{"p_pro": item.p_pro.pro, "p_en_pro": item.p_pro.en_pro, "p_result": item.p_num, "p_sum": item.p_sum} for item in bills]

        data = {"id": str(recorder.id).zfill(12),"obj_k": obj_k, "times": recorder.p_times, "total": recorder.p_result, "off_total": off}

        return render(request, "dayin.html", data)

        # for j in obj_k:
        #     if j.p_phone==None:
        # # if request.GET.get("phone")=="" and request.GET.get("phone")=="":
        #         return render(request, "dayin.html", {"obj_k": obj_k, "times": times, "total": total, "off_total":total})
        #     else:
        #         return render(request, "dayin.html", {"obj_k":obj_k,"times":times,"total":total,"off_total":off_total})

        # return render(request,"dayin.html",{"total":total,"p_pro":p_pro,"p_price":p_price,
        #                                     "p_result":p_result,"p_sum":p_sum})


##############################################################
def manager(request):
    if request.method == 'GET':
        return render(request, 'manager.html')
    elif request.method == 'POST':
        req = request.POST
        print(req)
        menu_name = req.get('menu_name')
        pro_id = req.get('pro_id')
        pro = req.get('pro')
        print(pro)
        en_pro = req.get('en_pro')
        price = req.get("price")
        img = req.get("img_addr")
        content = request.FILES.get("img", None)  # 图片
        msg = '修改成功'
        c = models.FoodList.objects.filter(menu_name=menu_name, pro_id=pro_id).count()
        print(c)
        if c and pro and en_pro and price and img:
            models.FoodList.objects.filter(menu_name=menu_name, pro_id=pro_id).update(pro=pro, en_pro=en_pro,
                                                                                      price=price, img=img)
            return render(request, "manager.html", {"msg": msg})
        if c and pro and en_pro and price:
            models.FoodList.objects.filter(menu_name=menu_name, pro_id=pro_id).update(pro=pro, en_pro=en_pro,
                                                                                      price=price)
            return render(request, "manager.html", {"msg": msg})
        if c and pro and en_pro:
            models.FoodList.objects.filter(menu_name=menu_name, pro_id=pro_id).update(pro=pro, en_pro=en_pro)
            return render(request, "manager.html", {"msg": msg})
        if c and price:
            models.FoodList.objects.filter(menu_name=menu_name, pro_id=pro_id).update(price=price)
            return render(request, "manager.html", {"msg": msg})
        if c and img:
            models.FoodList.objects.filter(menu_name=menu_name, pro_id=pro_id).update(img=img)
            return render(request, "manager.html", {"msg": msg})
        if menu_name and pro and pro_id and en_pro and price and img:
            models.FoodList.objects.create(
                menu_name=menu_name,
                pro_id=pro_id,
                pro=pro,
                en_pro=en_pro,
                price=price,
                img=img,
            )
            obj = models.FoodList.objects.all()
            for j in obj:
                # print(j.pro)
                return render(request, "manager.html", {"foodlist": j.pro})
        if not content:
            msg_img = "无图片上传"
            return render(request, "manager.html", {"msg_img": msg_img})
        position = os.path.join('/home/mrbison/PycharmProjects/diancan/static/diancan/images/index', content.name)
        # 获取上传文件的文件名，并将其存储到指定位置
        storage = open(position, 'wb+')  # 打开存储文件
        for chunk in content.chunks():  # 分块写入文件
            storage.write(chunk)
        storage.close()  # 写入完成后关闭文件
        # return HttpResponse("上传成功")  # 返回客户端信息
        return render(request, "manager.html")
    else:
        return HttpResponse("不支持的请求方法")


def diancan(request):
    if request.method == "GET":
        print(request.GET)
        # return render(request,"diancan.html")
        # count=models.DishesList.objects.filter(menu_name="菜品1").count()
        obj1 = models.FoodList.objects.filter(menu_name="菜品1")
        obj2 = models.FoodList.objects.filter(menu_name="菜品2")
        obj3 = models.FoodList.objects.filter(menu_name="菜品3")
        obj4 = models.FoodList.objects.filter(menu_name="菜品4")
        obj5 = models.FoodList.objects.filter(menu_name="菜品5")
        obj6 = models.FoodList.objects.filter(menu_name="菜品6")
        obj7 = models.FoodList.objects.filter(menu_name="菜品7")
        return render(request, "diancan.html", {"obj1": obj1, "obj2": obj2, "obj3": obj3, "obj4": obj4,"obj5": obj5,"obj6": obj6,"obj7": obj7 })

        ##########################################################


def leke(request):
    return render(request, "leke.html")

###################################
